package com.lottery.im.module.chat.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.lottery.im.R;
import com.lottery.im.base.BaseActivity;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.widget.photoview.PhotoView;
import java.io.File;

/**
 * 是否发送原图
 */
public class ChooseOriginalImageActivity extends BaseActivity {

  public final static String PATH_KEY = "PATH_KEY";
  public final static String ORIGINAL_KEY = "ORIGINAL_KEY";

  public static void create(Activity activity, String path, int requestCode) {
    Intent intent = new Intent(activity, ChooseOriginalImageActivity.class);
    intent.putExtra(PATH_KEY, path);
    activity.startActivityForResult(intent, requestCode);
  }

  @BindView(R.id.pic) PhotoView mPic;
  @BindView(R.id.iv_original) ImageView mIvOriginal;
  @BindView(R.id.tv_size) TextView mTvSize;

  private boolean isOriginal = false;

  private String mPath;

  @Override protected int getLayoutResId() {
    return R.layout.activity_chooseoriginalimage;
  }

  @Override protected void initPresenter() {
    initView();
  }

  private void initView() {
    StatusBarHelper.translucent(this);
    mPath = getIntent().getStringExtra(PATH_KEY);
    Glide.with(this).load(new File(mPath)).into(mPic);
    File file = new File(mPath);
    long fileLength = file.length();
    mTvSize.setText("原图（" + getFileSize(fileLength) + "）");
  }

  @OnClick({ R.id.iv_back, R.id.layout_original, R.id.btn_send })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        finish();
        break;
      case R.id.layout_original:
        isOriginal();
        break;
      case R.id.btn_send:
        send();
        break;
    }
  }

  private void send(){
    Intent data = new Intent();
    data.putExtra(PATH_KEY,mPath);
    data.putExtra(ORIGINAL_KEY,isOriginal);
    setResult(Activity.RESULT_OK,data);
    finish();
  }


  /**
   * 设置是否选择原图的icon
   */
  private void isOriginal() {
    if (!isOriginal) {
      mIvOriginal.setImageResource(R.drawable.icon_xz_selected);
    } else {
      mIvOriginal.setImageResource(R.drawable.icon_xz_nomal);
    }
    isOriginal = !isOriginal;
  }

  /**
   * 根据图片大小计算出转换为 B\K\M 后的值
   *
   * @param size 图片大小
   */
  private String getFileSize(long size) {
    StringBuilder strSize = new StringBuilder();
    if (size < 1024) {
      strSize.append(size).append("B");
    } else if (size < 1024 * 1024) {
      strSize.append(size / 1024).append("K");
    } else {
      strSize.append(size / 1024 / 1024).append("M");
    }
    return strSize.toString();
  }
}
